/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package onlinestore.GUI;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import onlinestore.user.user;

/**
 *
 * @author anxiaochen
 */
public class UserFrame extends JFrame implements ActionListener
{
    JLabel usernameJL = new JLabel("Username :");
    private JTextField usernameJT = new JTextField();
    JLabel passwordJL = new JLabel("Password :");
    JTextField passwordJT = new JTextField();
    JDialog ERROR = new JDialog();
    JLabel ERROR_MESSAGE = new JLabel("Can't find this user.");
    JButton ERROR_OK = new JButton("OK");
    JButton LOGIN = new JButton("Log in");
    JButton RIGISTER = new JButton("Rigister");
    user u = new user();
    public UserFrame()
    {
        RIGISTER.addMouseListener(new MouseListener() {

            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
        LOGIN.addMouseListener(new MouseListener() {
            public void mouseClicked(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
                System.out.println(usernameJT.getText());
                u.finUser(usernameJT.getText());
                System.out.println("u.getUsername()" + u.getUsername());
                System.out.println("usernameJT.getText()" + usernameJT.getText());
                if(usernameJT.getText().equals(u.getUsername())&&u.getPassword().equals(passwordJT))
                {
                    
                    HomeFrame h = new HomeFrame();
                    h.setVisible(true);
                    setVisible(false);
                }
                else
                {
                    System.out.println("u.getUsername()" + u.getUsername());
                    System.out.println("usernameJT.getText()" + usernameJT.getText());
                    System.out.println("u.getUsername()" + u.getPassword());
                    System.out.println("usernameJT.getText()" + passwordJT.getText());
                    ERROR.setSize(250,70);
                    ERROR.setTitle("ERROR");
                    Container dialogPane = ERROR.getContentPane();
                    
                    dialogPane.setLayout(new GridLayout());
                    dialogPane.add(ERROR_MESSAGE);
                    dialogPane.add(ERROR_OK);
                    ERROR_OK.addMouseListener(new MouseListener() {
                        public void mouseClicked(MouseEvent e) {
                        }

                        public void mousePressed(MouseEvent e) {
                            ERROR.dispose();
                        }

                        public void mouseReleased(MouseEvent e) {
                        }

                        public void mouseEntered(MouseEvent e) {
                        }

                        public void mouseExited(MouseEvent e) {
                        }
                    });
                    dialogPane.setBackground(Color.WHITE);
                    ERROR.setLocationRelativeTo(null);
                    ERROR.setVisible(true);
                }
            }

            public void mouseReleased(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }
        });
        add(usernameJL);
        add(usernameJT);
        add(passwordJL);
        add(passwordJT);
        add(LOGIN);
        add(RIGISTER);

        Container contentPane = getContentPane();
        contentPane.setBackground(Color.WHITE);
        contentPane.setLayout(new GridLayout());
        setTitle("User --- Online Store");
        setBounds(0, 0, 800, 600);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent e) {
        u.finUser(usernameJT.getText());
    }
}
